package messages;

import static org.junit.Assert.*;

import org.junit.Test;

public class MessageUtilsTest {

    @Test
    public void testParseInt()
    {
        
        byte[] buf = new byte[] { 16, 39, 0, 0 };
        assertEquals(10000, MessageUtils.parseInt(buf, 0));
        
        buf = new byte[] { -1, -1, -1, -1 };
        assertEquals(-1, MessageUtils.parseInt(buf, 0));
        
        buf = new byte[] { -1, 0, -1, -1 };
        assertEquals(-65281, MessageUtils.parseInt(buf, 0));
    }
    
    @Test
    public void testInsertInt()
    {
        byte[] buf = new byte[4];
        MessageUtils.insertInt(buf, 0, -65281);
        assertEquals(-1, buf[0]);
        assertEquals(0, buf[1]);
        assertEquals(-1, buf[2]);
        assertEquals(-1, buf[3]);
        
        buf = new byte[8];
        MessageUtils.insertInt(buf,  2, -1);
        
        assertEquals(0, buf[0]);
        assertEquals(0, buf[1]);
        assertEquals(-1, buf[2]);
        assertEquals(-1, buf[3]);
        assertEquals(-1, buf[4]);
        assertEquals(-1, buf[5]);
        assertEquals(0, buf[6]);
        assertEquals(0, buf[7]);
    }
    
    @Test
    public void testCycle()
    {
        int expectedVal;
        byte[] buf = new byte[4];
        int outVal;
        
        expectedVal = 10000;
        MessageUtils.insertInt(buf, 0, expectedVal);
        outVal = MessageUtils.parseInt(buf, 0);
        assertEquals(expectedVal, outVal);
        
        expectedVal = 40900000;
        MessageUtils.insertInt(buf, 0, expectedVal);
        outVal = MessageUtils.parseInt(buf, 0);
        assertEquals(expectedVal, outVal);
        
        expectedVal = -1;
        MessageUtils.insertInt(buf, 0, expectedVal);
        outVal = MessageUtils.parseInt(buf, 0);
        assertEquals(expectedVal, outVal);
        
        expectedVal = -8484732;
        MessageUtils.insertInt(buf, 0, expectedVal);
        outVal = MessageUtils.parseInt(buf, 0);
        assertEquals(expectedVal, outVal);
        
    }

}
